/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas;

import Gestores.DtoObjects.BitacoraDTO;
import Gestores.Entidades.EntidadBitacora;
import Gestores.Entidades.EntidadDetallePago;
import Gestores.Entidades.EntidadPagoCertificado;
import Gestores.Entidades.EntidadPagoCredencial;
import Negocios.CuotaNegocio;
import Negocios.DetallePagoNegocio;
import Util.Acciones;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lion
 */
//private int id_mes;
//    private int id_a;
//    private String id_afiliado;
//    private int id_usuario;
//    private int monto;
//    private Date fecha;
public class BuscarDetallePago extends javax.swing.JFrame {

    private DetallePago ventanareferencia;
    private DetallePagoNegocio negocio;
    private DefaultTableModel model;
    private String campos[] = {"Mes", "Anho", "Cod Afiliado", "Id Usuario", "monto", "Fecha"};
    private EntidadBitacora b;
    private BitacoraDTO dto;

    /**
     * Creates new form BuscarDetallePago
     */
    public BuscarDetallePago() {
        initComponents();
        this.negocio = new DetallePagoNegocio();
        this.model = new DefaultTableModel(campos, 0);
        this.table_datos.setModel(model);
        this.table_datos.enableInputMethods(false);
        this.dto = new BitacoraDTO();
        this.table_datos.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = table_datos.getSelectedRow();
                int col = table_datos.getSelectedColumn();
                DetallePagoNegocio negocio = new DetallePagoNegocio();
                EntidadDetallePago dp = new EntidadDetallePago();
                //{"Mes", "Anho", "Cod Afiliado", "Id Usuario", "monto", "Fecha"} 
                dp.setId_afiliado((String) table_datos.getValueAt(row, 2));
                dp.setId_usuario((Integer) table_datos.getValueAt(row, 3));
                dp.setMonto((Integer) table_datos.getValueAt(row, 4));

                dp.setId_mes(negocio.obtenerMesCodigo((String) table_datos.getValueAt(row, 0)));
                dp.setId_a((Integer) table_datos.getValueAt(row, 1));
                dp.setFecha((Date) table_datos.getValueAt(row, 5));
                ventanareferencia.cargarDatos(dp);
                BuscarDetallePago.this.dispose();
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        rbCodCuota = new javax.swing.JRadioButton();
        rbCodAfi = new javax.swing.JRadioButton();
        cmb_m = new com.toedter.calendar.JMonthChooser();
        cmb_a = new com.toedter.calendar.JYearChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txt_codafi = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        table_datos = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_mensaje = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterio De Busqueda"));

        rbCodCuota.setText("CodCuota");

        rbCodAfi.setText("CodAfiliado");

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Nuevo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbCodCuota)
                    .addComponent(rbCodAfi)
                    .addComponent(jButton2))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_codafi, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cmb_m, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmb_a, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(rbCodCuota)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbCodAfi))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmb_a, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmb_m, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_codafi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        table_datos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(table_datos);

        jLabel1.setText("Mensaje");

        txt_mensaje.setColumns(20);
        txt_mensaje.setRows(5);
        jScrollPane2.setViewportView(txt_mensaje);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.rbCodAfi.setSelected(false);
        this.rbCodCuota.setSelected(false);
        this.txt_codafi.setText("");
        this.txt_mensaje.setText("");

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (this.rbCodAfi.isSelected() && !this.rbCodCuota.isSelected()) {
            if (this.txt_codafi.getText() != null && this.txt_codafi.getText().length() < 6) {
                try {
                    ArrayList<EntidadDetallePago> a = this.negocio.BuscarDetallePagoIdAfi(this.txt_codafi.getText());
                    DefaultTableModel model = this.negocio.tabularDatos(a);
                    this.table_datos.setModel(model);
                    this.b = new EntidadBitacora("Criterio Busqueda por codigo afiliado "
                            + this.txt_codafi.getText(), Principal.Usuario,
                            Principal.CajaChica, Acciones.Select);
                    this.dto.insertar(b);
                } catch (Exception ex) {
                    this.txt_mensaje.setText(ex.getMessage());
                    Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                this.txt_mensaje.setText("Verificar codigo Afiliado, no debe estar vacio, y no debe ser mayor a 5 caracteres");
            }
        } else {
            if (!this.rbCodAfi.isSelected() && this.rbCodCuota.isSelected()) {
                ArrayList<EntidadDetallePago> a;
                try {
                    a = this.negocio.BuscarDetallePagoCuota(this.cmb_m.getMonth(), this.cmb_a.getYear());
                    DefaultTableModel model = this.negocio.tabularDatos(a);
                    this.table_datos.setModel(model);
                    this.b = new EntidadBitacora("Criterio Busqueda Cuota " + String.valueOf(this.cmb_m.getMonth()) + "-"
                            + String.valueOf(this.cmb_a.getYear()), Principal.Usuario,
                            Principal.CajaChica, Acciones.Select);
                    this.dto.insertar(b);
                } catch (Exception ex) {
                    this.txt_mensaje.setText(ex.getMessage());
                    Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                if (this.rbCodAfi.isSelected() && this.rbCodCuota.isSelected()) {
                    if (this.txt_codafi.getText() != "") {
                        EntidadDetallePago a;
                        try {
                            a = this.negocio.BuscarDetallePagoAfiCuota(this.cmb_m.getMonth(), this.cmb_a.getYear(), this.txt_codafi.getText());
                            if (a != null) {
                                ArrayList<EntidadDetallePago> l = new ArrayList<EntidadDetallePago>();
                                l.add(a);
                                DefaultTableModel model = this.negocio.tabularDatos(l);
                                this.table_datos.setModel(model);
                                this.b = new EntidadBitacora("Criterio Busqueda Cuota " + String.valueOf(this.cmb_m.getMonth()) + "-"
                                        + String.valueOf(this.cmb_a.getYear()) + " y cod afiliado " + this.txt_codafi.getText(), Principal.Usuario,
                                        Principal.CajaChica, Acciones.Select);
                                this.dto.insertar(b);
                            } else {

                                this.txt_mensaje.setText("El afiliado con codigo " + this.txt_codafi.getText() + " no ha realizado el pago de la cuota"
                                        + String.valueOf(this.cmb_m.getMonth()) + "-" + String.valueOf(this.cmb_a.getYear()));
                            }

                        } catch (Exception ex) {
                            this.txt_mensaje.setText(ex.getMessage());
                            Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } else {
                        this.b = new EntidadBitacora("Error al buscar cod afiliado " + this.txt_codafi.getText(), Principal.Usuario,
                                Principal.CajaChica, Acciones.Select);
                        try {
                            this.dto.insertar(b);
                        } catch (Exception ex) {
                            Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        this.txt_mensaje.setText("Verificar codigo Afiliado, no debe estar vacio, y no debe ser mayor a 5 caracteres");
                    }
                } else {
                    this.b = new EntidadBitacora("Error al buscar NO selecciono un criterio", Principal.Usuario,
                            Principal.CajaChica, Acciones.Select);
                    try {
                        this.dto.insertar(b);
                    } catch (Exception ex) {
                        Logger.getLogger(BuscarDetallePago.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    this.txt_mensaje.setText("Debe Seleccionar una Opcion");
                }
            }

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BuscarDetallePago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BuscarDetallePago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BuscarDetallePago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BuscarDetallePago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BuscarDetallePago().setVisible(true);


            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JYearChooser cmb_a;
    private com.toedter.calendar.JMonthChooser cmb_m;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rbCodAfi;
    private javax.swing.JRadioButton rbCodCuota;
    private javax.swing.JTable table_datos;
    private javax.swing.JTextField txt_codafi;
    private javax.swing.JTextArea txt_mensaje;
    // End of variables declaration//GEN-END:variables

    void enlazar(DetallePago aThis) {
        this.ventanareferencia = aThis;
    }
}
